package com.demon.auto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demon.auto.dao.SignRecordDao;
import com.demon.auto.model.entity.SignRecordEntity;
import com.demon.auto.model.vo.PageVo;
import com.demon.auto.model.vo.SignRecordVo;
import com.demon.auto.service.SignRecordService;
import org.springframework.stereotype.Service;

/**
 * 签到记录
 *
 * @version 1.0
 * @author huangrz
 * @date 2025/02/13 17:08
 */
@Service
public class SignRecordServiceImpl extends ServiceImpl<SignRecordDao, SignRecordEntity> implements SignRecordService {

    @Override
    public boolean saveSignResult(SignRecordEntity signRecordEntity) {
        if (signRecordEntity.getId() == null) {
            return false;
        }
        LambdaUpdateWrapper<SignRecordEntity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SignRecordEntity::getId, signRecordEntity.getId())
                .set(SignRecordEntity::getSignTime, signRecordEntity.getSignTime())
                .set(SignRecordEntity::getStatusCode, signRecordEntity.getSignTime() == null ? 0 : 1)
                .set(SignRecordEntity::getDetailInfo, signRecordEntity.getDetailInfo());
        return update(updateWrapper);
    }

    @Override
    public PageVo<SignRecordEntity> queryByPage(SignRecordVo condition) {
        LambdaQueryWrapper<SignRecordEntity> queryWrapper = Wrappers.lambdaQuery();
        if (condition.getClientId() != null) {
            queryWrapper.eq(SignRecordEntity::getClientId, condition.getClientId());
        }
        if (condition.getStatusCode() != null) {
            queryWrapper.eq(SignRecordEntity::getStatusCode, condition.getStatusCode());
        }
        if (condition.getSignTimeBegin() != null && condition.getSignTimeEnd() != null) {
            queryWrapper.between(SignRecordEntity::getSignTime, condition.getSignTimeBegin(), condition.getSignTimeEnd());
        }
        if (condition.getCreateTimeBegin() != null && condition.getCreateTimeEnd() != null) {
            queryWrapper.between(SignRecordEntity::getCreateTime, condition.getCreateTimeBegin(), condition.getCreateTimeEnd());
        }
        queryWrapper.orderByDesc(SignRecordEntity::getCreateTime);
        return baseMapper.selectPage(condition, queryWrapper);
    }

}
